package com.situ.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.web.common.ServerResponse;
import com.situ.web.entity.Admin;
import com.situ.web.service.ILoginService;
import com.situ.web.service.impl.LoginServiceImpl;
import com.situ.web.util.MD5Util;

@Controller
@RequestMapping("/loginController")
public class LoginController {
	
	private ILoginService loginService = new LoginServiceImpl();
	

	@RequestMapping(value = "/logout")
	private String logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		HttpSession session = req.getSession();
		Admin admin = (Admin) session.getAttribute("admin");
		if(admin != null) {
			List<Admin> onLineAdminList = (List<Admin>) req.getSession().getAttribute("onLineAdminList");
			onLineAdminList.remove(admin);
			
			session.invalidate();
			return "redirect:/login.jsp";
		} else {
			return null;
		}
		
	}

	@RequestMapping(value = "/login")
	@ResponseBody
	private ServerResponse login(HttpServletRequest req,HttpServletResponse resp) throws IOException{
		
		String checkCode = req.getParameter("checkCode");
		String checkCodeSession = (String) req.getSession().getAttribute("checkCodeSession");
		 if("" == checkCode
		   || null == checkCode
		   || checkCode.equals(checkCodeSession) == false) {
			 ServerResponse serverResponse = ServerResponse.createError("验证码错误");
		     return serverResponse;
		} 
		
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		String enPassword = MD5Util.EncodeUtf8(password);
		Admin admin = loginService.login(name,enPassword);
		
		if (null != admin) { // 登录成功
			HttpSession session = req.getSession();
			session.setAttribute("admin", admin);
			
			List<Admin> onLineAdminList = (List<Admin>) req.getSession().getAttribute("onLineAdminList");
			// 第一个用户登录时候这个list是null
			if (null == onLineAdminList) {
				onLineAdminList = new ArrayList<Admin>();
				req.getSession().setAttribute("onLineAdminList", onLineAdminList);
			}
			onLineAdminList.add(admin);
			
			ServerResponse serverResponse = ServerResponse.createSuccess("登陆成功");
			return serverResponse;
		} else {
			ServerResponse serverResponse = ServerResponse.createError("用户名或密码错误");
			return serverResponse;
		}
		
	}
	
	@RequestMapping("/register")
	@ResponseBody
	private ServerResponse register(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		String email = req.getParameter("email");
		String password = req.getParameter("password");
		
		Admin admin = loginService.selectByName(name);
		ServerResponse serverResponse = null;
		
		if (null == admin) { // 用户不存在
			
			Admin emailAdmin = loginService.selectByEmail(email);
			
			if (null != emailAdmin) {
				serverResponse = serverResponse.createError("邮箱已被注册");
				return serverResponse;
			}
			
			String enPassword = MD5Util.EncodeUtf8(password);
			
			boolean isSuccess = loginService.register(name,email,enPassword);
			
			if (isSuccess) {
				serverResponse = ServerResponse.createSuccess("注册成功");
				return serverResponse;
			} else {
			    serverResponse = ServerResponse.createError("注册失败");
			    return serverResponse;
			}
		} else {
			serverResponse = ServerResponse.createError("用户名已存在");
			return serverResponse;
		}
	}
}
